package lastcontrnt.memoryleak;

import java.util.ArrayList;
import java.util.List;

/**
 * 监听器没有及时清理导致内存泄漏
 */
public class Leak8 {
    private List<Runnable> listeners = new ArrayList<>();

    public void addListener(Runnable listener) {
        listeners.add(listener);
    }

    public void triggerEvent() {
        for (Runnable listener : listeners) {
            listener.run();
        }
    }

    private void remove() {
        listeners.clear();
    }

    public static void main(String[] args) {
        Leak8 publisher = new Leak8();
        publisher.addListener(() -> System.out.println("Listener triggered."));
        publisher.triggerEvent();

        // 没有移除监听器，可能导致内存泄漏
        //publisher.remove();
    }


}
